/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package UTILITARIOS;

/**
 *
 * @author Marco Antonio
 */
public class EncriptarCadena {

     

                public EncriptarCadena()
                {
                }

                public String Encriptar(String c, String t, boolean a)
                {
                    String res="";

                    int i=0, j=0, ic=c.length()    ,it=t.length();
                               int temp;
                               int[] claveascii = new int[ic];
                               int[] textoascii = new int[it];

                               for (i=0; i<ic;i++)  claveascii[i]=c.charAt(i);

                               for (i=0; i<it;i++)  textoascii[i]=t.charAt(i);

                               if(a)
                               {
                                               for (i=0; i<it;i++)
                                               {
                                                               j++;
                                                               if (j>=ic)j=0;

                                                               temp= textoascii[i]+claveascii[j];
                                                               if (temp > 255) temp=temp-255;
                                                               res=res + (char)temp;

                                               }
                               }
                               else
                               {
                                               for (i=0; i<it;i++)
                                               {
                                                               j++;
                                                               if (j>=ic)j=0;

                                                               temp= textoascii[i]-claveascii[j];
                                                               if (temp < 0) temp=temp+256;
                                                               res=res + (char)temp;

                                               }

                               }


                    return res;
                }

                public static void main(String[] args) {
                    EncriptarCadena c = new EncriptarCadena();
                    System.out.println("resultado ="+c.Encriptar("nfn inversiones", "INGRESAR", true));
                    System.out.println("resultado ="+c.Encriptar("hola", "ÜáÏÌÞ", false));
                    
                }

}
